$(document).ready(function(){
    //Add
    $('.sym_light_menu_admin .symbiosis-button-add').click(function(){
        if($('#sym_light_menu_admin_add').length){
            $('#sym_light_menu_admin_add').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_light_menu_admin_add" title="Menu adding"></div>');
            $('#sym_light_menu_admin_add').dialog({
                modal: true,
                width: 270,
                position: ['center', 100],
                open: function(event, ui) {
                    $.ajax({
                        data:{
                            symbiont: 'light_menu-admin.add'
                        },
                        success:function(r){
                            $('#sym_light_menu_admin_add').html(r).sym();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        var name=$('#sym_light_menu_admin_add .name').val();
                        var d=$(this);
                        $.ajax({
                            data:{
                                symbiont: 'light_menu-admin.db_add',
                                name: name
                            },
                            success:function(r){
                                d.dialog("close");
                                location.reload(true);
                            }
                        });
                    }
                }
            });
        }
    });
    //Edit
    $('.sym_light_menu_admin .edit').click(function(){
        t=this;
        if($('#sym_light_menu_admin_edit').length){
            $('#sym_light_menu_admin_edit').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_light_menu_admin_edit" title="Menu editing"></div>');
            $('#sym_light_menu_admin_edit').dialog({
                modal: true,
                width: 270,
                position: ['center', 100],
                open: function(event, ui) {
                    var id=$(t).parent().find('.id').text();
                    $.ajax({
                        data:{
                            symbiont: 'light_menu-admin.edit.'+id
                        },
                        success:function(r){
                            $('#sym_light_menu_admin_edit').html(r).sym();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        var id=$(t).parent().find('.id').text();
                        var name=$('#sym_light_menu_admin_edit .name').val();
                        var d=$(this);
                        $.ajax({
                            data:{
                                symbiont: 'light_menu-admin.db_edit',
                                id: id,
                                name: name
                            },
                            success:function(r){
                                d.dialog("close");
                                location.reload(true);
                            }
                        });
                    }
                }
            });
        }
    });
    //Delete
    $('.sym_light_menu_admin .delete').click(function(){
        t=$(this);
        if($('#sym_light_menu_admin_delete').length){
            $('#sym_light_menu_admin_delete').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_light_menu_admin_delete" title="Menu deleting"></div>');
            $('#sym_light_menu_admin_delete').dialog({
                modal: true,
                width: 270,
                position: ['center', 100],
                open: function(event, ui) {
                    var id=$(t).parent().find('.id').text();
                    $.ajax({
                        data:{
                            symbiont: 'light_menu-admin.delete.'+id
                        },
                        success:function(r){
                            $('#sym_light_menu_admin_delete').html(r).sym();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        var id=$(t).parent().find('.id').text();
                        var d=$(this);
                        $.ajax({
                            data:{
                                symbiont: 'light_menu-admin.db_delete',
                                id: id
                            },
                            success:function(r){
                                d.dialog("close");
                                t.parent().slideUp();
                            }
                        });
                    }
                }
            });
        }
    });
});